<?php

use Plugins\Edge\Admin\Controller\NewsController;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;
use Plugins\Edge\Common\Enums\NewsEnumIsTop;
use Plugins\Edge\Common\Enums\NewsEnumStatus;

Html::create('新闻资讯列表');

$table = Table::create(TP::route()->to([NewsController::class, 'listsData'])->url(), 'news');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('ID', 'id'),
    Table\Column::normal('标题', 'title'),
    Table\Column::normal('简介', 'intro'),
    Table\Column::normal('封面', 'cover')->showImage(),
    Table\Column::normal('浏览数', 'view_number'),
    Table\Column::normal('作者', 'author'),
    Table\Column::normal('置顶', 'is_top')->showSwitch(NewsEnumIsTop::mapping(),  TP::route()->to([NewsController::class, 'switchToggle'])),
    Table\Column::normal('状态', 'status')->showSwitch(NewsEnumStatus::mapping(),  TP::route()->to([NewsController::class, 'switchToggle'])),
    Table\Column::normal('创建事件', 'create_time'),
);


$table->setHeaderEvent("@primary.Refresh.刷新数据", "this.newsGetData()");

// ===========================================
// 新页面: 注释代码为Vue组件模式， 默认为iframe模式
// ===========================================

//$component = Temporary::create('create')->setContent(include __DIR__ . '/form.sc.php');

//$table->setHeaderEvent('@primary.Plus.新增',
//    Table\EventHandler::window('新增')->setConfig(['width' => '500px'])->setComponent($component)
//);

//$table->setRowEvent('@primary.Edit.编辑',
//    Table\EventHandler::window('编辑')->setConfig(['width' => '500px'])->setComponent($component)
//);


$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setConfig(['width' => '1000px'])->setUrl( TP::route()->to([NewsController::class, 'create']))
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑')->setConfig(['width' => '1000px'])->setUrl( TP::route()->to([NewsController::class, 'edit']), ['id' => '@id'])
);
$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: TP::route()->to([NewsController::class, 'destroy']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.newsGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);
return $table->render();
